﻿/***********************************************************************
 * <copyright file="SqlServerMembershipDao.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, March 14, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Data;
using BusinessSecurity;
using DataObjects;
using DataObjects.ADO;
using DataSecurity.StandartInterfaces;

namespace DataSecurity.ADO.SqlServer.Dictionary
{
    /// <summary>
    /// Defines methods to access Membership
    /// </summary>
    /// <remarks>
    /// This is a database-independent class. SqlServermplementations are database specific
    /// </remarks>
    public class SqlServerMembershipDao : IMembership
    {
        /// <summary>
        /// Gets the membership.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public Membership Get(int userId)
        {
            const string sql = @"uspGet_Membership";
            object[] parms = { "@UserId", userId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        private static readonly Func<IDataReader, Membership> Make = reader =>
          new Membership
          {
              UserId = reader["UserId"].AsInt(),
              CreateDate = reader["CreateDate"].AsDateTime(),
              ConfirmationToken = reader["ConfirmationToken"].AsString(),
              IsConfirmed = reader["IsConfirmed"].AsBool(),
              LastPasswordFailureDate = reader["LastPasswordFailureDate"].AsDateTime(),
              PasswordFailuresSinceLastSuccess = reader["PasswordFailuresSinceLastSuccess"].AsInt(),
              Password = reader["Password"].AsString(),
              PasswordChangedDate = reader["PasswordChangedDate"].AsDateTime(),
              PasswordSalt = reader["PasswordSalt"].AsString(),
              PasswordVerificationToken = reader["PasswordVerificationToken"].AsString(),
              PasswordVerificationTokenExpirationDate = reader["PasswordVerificationTokenExpirationDate"].AsDateTime()
          };

    }
}
